package com.good.child.http;

import static com.good.child.config.RequestID.Community.ADD_COMMUNITY_LIST;

import android.content.Context;
import android.text.TextUtils;

import com.good.child.R;
import com.good.child.bean.BaseResponse;
import com.good.child.bean.ai.AIHelperHistoryBean;
import com.good.child.bean.ai.CommunityBean;
import com.good.child.config.Api;
import com.good.child.config.RequestID;
import com.lzy.okgo.model.Response;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.http.BaseReqBean;
import com.yimidida.library.http.LoadingBaseObserver;
import com.yimidida.library.tool.JsonSerializer;
import com.yimidida.library.utils.LogUtils;
import com.yimidida.library.utils.ToastUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create on: 2024/8/21
 * by Mr.Zhu
 * Description: 社区
 */
public class AIMessageLogical extends AppBaseLogical {

    private static AIMessageLogical instance;

    public static AIMessageLogical getInstance() {
        if (instance == null) {
            synchronized (AIMessageLogical.class) {
                if (instance == null) {
                    instance = new AIMessageLogical();
                }
            }
        }
        return instance;
    }

    /**
     * 消息类型 type ，0为AI助手，1为AI文生图，5为图生图(AI相机),4图片识别
     *
     * @param context
     * @param pageNum
     * @param pageSize
     * @param type
     */
    public void requestAIHistory(Context context, String pageNum, String pageSize, String type, ICallBackResultListener resultListener) {
        String url = Api.AIHistory.AI_HELPER_HISTORY;
        BaseReqBean reqBean = new BaseReqBean();
        reqBean.getMap().put("pageNum", pageNum);
        reqBean.getMap().put("pageSize", pageSize);
        reqBean.getMap().put("type", type);
        requestStringForLogin(url, reqBean.getMap(), new AIMessageObserver(context, RequestID.Community.AI_HELPER_HISTORY_LIST, resultListener));
    }

    private class AIMessageObserver extends LoadingBaseObserver {
        private ICallBackResultListener resultListener;
        private int requestID;

        public AIMessageObserver(Context activity, int requestID, ICallBackResultListener resultListener) {
            this.resultListener = resultListener;
            this.requestID = requestID;
            setShowLoading(activity);
        }

        @Override
        public void onNext(Response<String> stringResponse) {
            super.onNext(stringResponse);
            if (stringResponse == null || stringResponse.getRawResponse().code() != SUCCESS_CODE || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail);
                return;
            }
            String bodyString = stringResponse.body();
            try {
                switch (requestID) {
                    case RequestID.Community.AI_HELPER_HISTORY_LIST:
                        LogUtils.showLog("ai助手 ：" + bodyString);
                        AIHelperHistoryBean aiHelperHistoryBean = JsonSerializer.deSerialize(bodyString, AIHelperHistoryBean.class);
                        if (aiHelperHistoryBean.code == 200) {
                            if (resultListener != null) {
                                resultListener.onCallBack(aiHelperHistoryBean);
                            }
                        } else {
                            ToastUtil.showLong(aiHelperHistoryBean.msg);
                            if (resultListener != null) {
                                resultListener.onCallBack(null);
                            }
                        }
                        break;

                }
            } catch (Exception e) {
                ToastUtil.showLong(R.string.data_exception);
                if (resultListener != null) {
                    resultListener.onCallBack(null);
                }
            }

        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            ToastUtil.showLong(R.string.data_error);
            if (resultListener != null) {
                resultListener.onCallBack(null);
            }
        }
    }
}
